package com.house365.core.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Projection;
import com.house365.app.analyse.HouseAnalyse;
import com.house365.app.analyse.data.AnalyseMetaData;
import com.house365.core.R;
import com.house365.core.action.ActionTag;
import com.house365.core.anim.AnimBean;
import com.house365.core.anim.AnimCommon;
import com.house365.core.application.BaseApplication;
import com.house365.core.image.AsyncImageLoader;
import com.house365.core.image.CacheImageUtil;
import com.house365.core.view.LoadingDialog;


/**
 *@author py
 *@date 2012-8-28
 *@comment
 */
public abstract class BaseMenuWithGoogleMapActivity extends MapActivity {

	protected Activity thisInstance; 
	protected BaseApplication mApplication;
	protected AsyncImageLoader mAil;
	protected LoadingDialog tloadingDialog ;
	boolean mVisibleFromClient = true;
	
	private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		thisInstance =this;
		registerReceiver(mLoggedOutReceiver, new IntentFilter(
				ActionTag.INTENT_ACTION_LOGGED_OUT));
		mApplication = (BaseApplication) getApplication();
		preparedCreate(savedInstanceState);
	}
	protected abstract void preparedCreate(android.os.Bundle savedInstanceState);
	
	public AsyncImageLoader getImageLoader() {
		if (mAil == null) {
			mAil = new AsyncImageLoader(this);
		}
		return mAil;
	}
	public void setImage(final ImageView imageView, String imageUrl,
			final int resId,int scaleType) {CacheImageUtil.setCacheImage(imageView, imageUrl, resId, scaleType, getImageLoader());}


	public void invalidate(String imageUrl) {
		getImageLoader().invalidate(imageUrl);
	}

	protected void onDestroy() {
		super.onDestroy();
		if(mAil!= null){
			mAil.clearCacheImage();
		}
		
		unregisterReceiver(mLoggedOutReceiver);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	AnalyseMetaData analyseMetadata ;
	protected void onResume() {
		super.onResume();
		analyseMetadata = HouseAnalyse.onPageResume(this);
	}
	
	protected void onPause() {
		super.onPause();
		HouseAnalyse.onPagePause(analyseMetadata);
	}

	public abstract MapView getMapView();
	
	public enum GeoPointSceen{
			TOP_LEFT,
			BOTTOM_RIGHT
		
	}
	private LoadingDialog getLoadingDialog(){
		if(tloadingDialog == null){
			tloadingDialog= new LoadingDialog(this,R.style.dialog, R.string.loading);
			tloadingDialog.setCancelable(isCancelDialog());
			if(isCancelDialog()){
				tloadingDialog.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel(DialogInterface dialog) {
						onExitDialog();
					}
				});
			}
		
		}
		return tloadingDialog;
	}
	public void showLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().show();
		}
	}
	public void showLoadingDialog(int resid){
		if(getLoadingDialog() != null){
			getLoadingDialog().setMessage(this.getResources().getString(resid));
			getLoadingDialog().show();
		}
	}
	public void dismissLoadingDialog(){
		if(getLoadingDialog() != null){
			getLoadingDialog().dismiss();
		}
	}
	public boolean isCancelDialog(){
		return true;
	}
	public void onExitDialog(){
		thisInstance.finish();
	}
	/**
	 * get GeoPoint from screen
	 * @param screenType
	 * @return
	 */
	public GeoPoint getGeoPointOfScreen(GeoPointSceen screenType){
		Projection projection = getMapView().getProjection();
		if(screenType == GeoPointSceen.TOP_LEFT){
			return projection.fromPixels(0, 0);
		}else if(screenType == GeoPointSceen.BOTTOM_RIGHT){
			int w=0,h =0;
			if(getMapView().getWidth() != 0){
				w = getMapView().getWidth();
				h= getMapView().getHeight();
			}
			if(w ==0  && getMapView().getMeasuredWidth() !=0){
				w = getMapView().getMeasuredWidth();
				h = getMapView().getMeasuredHeight();
			}
			if(w ==0){
				w = mApplication.getMetrics().widthPixels;
				h = mApplication.getMetrics().heightPixels;
			}
			return  projection.fromPixels(w, h);
		}
		return projection.fromPixels(0, 0);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void startActivity(Intent intent) {
		super.startActivity(intent);
		AnimBean animBean = getStartAnim();
		if(animBean!= null && animBean.getIn()>0 && animBean.getOut()>0){
			AnimCommon.set(animBean.getIn(),animBean.getOut());
		}
	}
	@Override
	public void startActivityForResult(Intent intent, int requestCode) {
		// TODO Auto-generated method stub
		super.startActivityForResult(intent, requestCode);
		AnimBean animBean = getStartAnim();
		if(animBean!= null && animBean.getIn()>0 && animBean.getOut()>0){
			AnimCommon.set(animBean.getIn(),animBean.getOut());
		}
	}
	public AnimBean getStartAnim(){
		return new AnimBean(R.anim.slide_in_bottom,R.anim.slide_fix);
	}
	
}

